/*
 * @Author: Wangjun
 * @Date: 2023-08-14 10:40:47
 * @LastEditTime: 2023-09-06 09:18:54
 * @LastEditors: Wangjun
 * @Description:
 * @FilePath: \nodex\nodecmp\op_cmp.go
 * hnxr
 */
package nodecmp

import (
	"errors"

	"gitee.com/hnxr_1/nodex"
)

//0:> 1>= 2 < 3:<= 4== 5 !=
//M

func init() {
	nodex.Reister(new(Cmp))
}

type Cmp struct {
	nodex.Node
	X1 nodex.Double  `remark:"输入1"`
	X2 nodex.Double  `remark:"输入2"`
	M  nodex.Double  `remark:"0:>; 1:>=; 2:<; 3:<=; 4:== 5: !="`
	Y  nodex.Boolean `remark:"输出Y,,out"`
	f  nodex.FuncCmp
}

func (m *Cmp) Init(mem *nodex.Memory) (err error) {
	m.f, err = nodex.GetFuncCmp(m, int(m.M.Value))
	if err != nil {
		m.Bad = true
	}
	return
}

func (m *Cmp) GetModelID() int {
	return nodex.ModCmp
}

func (m *Cmp) GetName() string {
	return "CMP"
}

/**
 * @description: 节点信息
 * @return {*}
 */
func (m *Cmp) RegData() []byte {
	return nodex.MakeRegData(m, "比较运算", m.GetName(), "")
}

// //0:> 1>= 2 < 3:<= 4== 5 !=
func (m *Cmp) Calc() (err error) {
	if m.f == nil {
		err = errors.New(m.GetName() + ":模式M设置错误")
		return
	}
	if m.EnQ {
		if *m.X1.Bad || *m.X2.Bad || *m.M.Bad {
			m.Bad = true
			return //不计算
		}
	}
	m.Y.Value = m.f(m.X1.Value, m.X2.Value)

	m.Bad = false
	return
}
